<?php

namespace app\admin\traits;

trait SettingForm
{

    protected function getFieldForm($type, $name, $field, $content, $option)
    {

        $fieldClass = '\\generate\\field\\' . parse_name($type === 'switch' ? 'switch_field' : $type, 1, true);
        $form       = $fieldClass::$html;
        switch ($type) {
            case 'switch':
                $content_int = (int)$content;
                $search1     = "{if(isset(\$data) && \$data.[FIELD_NAME]==1)}checked{/if}";
                $search2     = "{\$data.[FIELD_NAME]|default='[FIELD_DEFAULT]'}";
                $form        = str_replace(array($search1, $search2, '[ON_TEXT]', '[OFF_TEXT]'), array($content ? 'checked' : '', $content_int, '是', '否',), $form);
                break;

            case 'select':
                $option_html = '';
                $option      = explode("\n", $option);
                foreach ($option as $item) {
                    $option_key_value = explode('||', $item);
                    $select = '';
                    if ($content == $option_key_value[0]) {
                        $select = 'selected';
                    }
                    $option_html .= '<option value="' . $option_key_value[0] . '" ' . $select . '>' . $option_key_value[1] . '</option>';
                }

                $form = str_replace('[RELATION_DATA]', $option_html, $form);
                break;

                case 'image':
                    $search1 = "{if !isset(\$data.[FIELD_NAME]) || \$data.[FIELD_NAME] == ''} style='display: none;'  {/if}";

                    $form    = str_replace([$search1], [($content == '') ? "style='display: none;'" : ''], $form);
    
                    break;
            default:
                //$form = '';

                break;
        }
     
        $form_value = "{\$data.[FIELD_NAME]|default='[FIELD_DEFAULT]'}";

        return str_replace(array($form_value, '[FIELD_NAME]', '[FORM_NAME]',), array($content, $field, $name,), $form);
    }
}
